# -*- coding:utf-8 -*-
__author__ = 'weikai'
from request_epicc.log import Logger
import threading,datetime,traceback
from  request_cic.request_getdata import getData,getData_CPlateNo
from request_cicc.cicc_standard_flow.flow_main import flow_main
from session import getSession
from request_cic.getCode import get_citycode
from request_pingan.entrance import pingan_request
from gevent import monkey; monkey.patch_socket()
monkey.patch_all()
monkey.patch_ssl()
import gevent
import thread
import json
log=Logger()

#车架号查询信息之后查询保费
def getSource(dt):
   try:
    sessiondt=""
    sessiondt = getSession()
    requestcic = sessiondt['4']
    loginRes=sessiondt['5']
    pingansession=sessiondt['1']

    vinNo=dt.get('vinNo','')
    insureCarId=dt.get('insureCarId','')
    if vinNo=='' or insureCarId=='':
        return
    dt['endDate']=dt.get("preInsureEndDate","")

    if dt.get('identitCard','')=='':
        dt['identitCard']='320123199106102810'
    dt['insuranceType']=dt.get("insuranceType","")
    client=dt['client']

    log.info(u"程序开始运行%s" % datetime.datetime.now())
    try:
        threads = []
        th = threading.Thread(target=getData, args=(requestcic, dt))
        th.start()
        threads.append(th)
        th = threading.Thread(target=flow_main, args=(loginRes, dt))
        th.start()
        threads.append(th)
        th = threading.Thread(target=pingan_request, args=(pingansession, dt))
        th.start()
        threads.append(th)
        for th in threads:
            th.join()
        print "程序结束运行%s" % datetime.datetime.now()

    except Exception,e:
        log.error(e)
        log.error(traceback.format_exc())

   except Exception as e:
        log.error(e)
        log.error(traceback.format_exc())

#根据车牌号进行查询保费信息
def plateNo_getSource(dt):
   try:
    requestcic=dt['4']
    loginRes=dt['5']
    pingansession=dt['1']


    cdt={}
    cdt['identitCard']=dt['identifyNumber']
    cdt['custName']=dt['insuredName']
    cdt['plateNumber']=dt['licenseNo']
    cdt['vinNo']=dt['vinNo']
    cdt['engineNo']=dt['engineNo']
    cdt['insureCarId']=dt['insureCarId']
    cdt['cityCode']=get_citycode(dt['licenseNo'])
    cdt['endDate']=dt['endDate']
    cdt['firstRegister']=dt['enrollDate']
    cdt['vehicleBrand']=dt['brandName']
    cdt['NSeatNum']=dt.get('NSeatNum',999)
    cdt['client']=dt['client']
    client=cdt['client']
    cdt['sessionId']=dt['sessionId']
    cdt['isPhone']=dt['isPhone']
    cdt['insuranceType']=dt['insuranceType']
    #cdt['insureTypeGroupId']=dt['insureTypeGroupId']

    log.info(u"程序开始运行%s" % datetime.datetime.now())
    try:
        #使用多线程调用
        threads = []
        th = threading.Thread(target=getData_CPlateNo, args=(requestcic, cdt))
        th.start()
        threads.append(th)
        th = threading.Thread(target=flow_main, args=(loginRes, cdt))
        th.start()
        threads.append(th)
        th = threading.Thread(target=pingan_request, args=(pingansession, cdt))
        th.start()
        threads.append(th)
        for th in threads:
            th.join()
        print "程序结束运行%s" % datetime.datetime.now()
        # g2 = gevent.spawn(getData_CPlateNo, requestcic,cdt)
        # g3 = gevent.spawn(flow_main, loginRes,cdt)
        # g4 = gevent.spawn(pingan_request, pingansession,cdt)
        # gevent.joinall([g2,g3,g4])
        #pingan_request(pingansession,cdt)
        #getData_CPlateNo(requestcic,cdt)
    except Exception,e:
        log.error(e)
        log.error(traceback.format_exc())

   except Exception as e:
        log.error(e)
        log.error(traceback.format_exc())
   log.info(u"程序结束运行%s" % datetime.datetime.now())

